package org.icrisat.mbdt.ui.wizards;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.icrisat.mbdt.database.DBConnection;

public class LoadDBImportWizardPage extends WizardPage {
	
	private Combo combo;
	private Combo comboGen;
	private Label lblGen;
	Connection con = null;
	Statement stmt = null;
	ResultSet rs = null;
	Statement stmt1 = null;
	ResultSet rs1 = null;
	
	
	protected LoadDBImportWizardPage(String pageName) {
		super(pageName);
		// TODO Auto-generated constructor stub
		setTitle("Load Data from DataBase");
	}


	public void createControl(Composite parent) {
		// TODO Auto-generated method stub
		setPageComplete(true);
		Composite container = new Composite(parent,SWT.None);
		container.setLayout(new GridLayout(2,false));
		
		Label lbl = new Label(container,SWT.NONE);
		lbl.setText("Project");
		lbl.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT));
		lbl.setLayoutData(new GridData(120,15));
		
		combo = new Combo(container,SWT.READ_ONLY | SWT.DROP_DOWN);
		combo.setLayoutData(new GridData(150, 35));
		
		lblGen = new Label(container,SWT.NONE);
		lblGen.setText("Generation");
		lblGen.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT));
		lblGen.setLayoutData(new GridData(120,15));
		lblGen.setEnabled(false);
		
		comboGen = new Combo(container,SWT.READ_ONLY | SWT.DROP_DOWN);
		comboGen.setLayoutData(new GridData(150, 35));
		comboGen.setEnabled(false);
		
		final DBConnection dbConnection = new DBConnection();
		
		try {
			
			con = dbConnection.connection();
			stmt = con.createStatement();			
			
			rs = stmt.executeQuery("select * from project_details order by project_id");
			while(rs.next()){
				combo.add(rs.getString("project_name"));
			}
			combo.addSelectionListener(new SelectionAdapter(){

				@Override
				public void widgetSelected(SelectionEvent e) {
					// TODO Auto-generated method stub
					try {
						if(comboGen.getText() != null){
							comboGen.removeAll();
						}
						con = dbConnection.connection();						
						stmt1 = con.createStatement();
						
						rs1 = stmt1.executeQuery("select * from project_details project inner join generation_details gen on project.project_id = gen.project_id where project.project_name='"+combo.getText().toString()+"'");
						while(rs1.next()){
							comboGen.add(rs1.getString("generation_name"));
						}
						lblGen.setEnabled(true);
						comboGen.setEnabled(true);
						
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}finally{
						try{
							if(con != null){
								con.close();					
							}
						}catch(Exception e2){
							e2.printStackTrace();
						}
					}
					
				}
				
			});
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try{
				if(con != null){
					con.close();					
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		setControl(container);
	}
	
	public String getSelectedProject(){
		String result = "";
	
		result = combo.getText()+"!@!"+comboGen.getText();
		
		return result;
	}
	
}
